package controller;

import java.util.ArrayList;

import server.ClientData;
import server.ClientState;
import server.IProtocolHandler;
import xml.Message;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import entity.User;

/**
 * 
 * @author fcarnevale
 */
public class CreateEventController implements IProtocolHandler {

	
	@Override
	public Message process(ClientState state, Message request) {
		entity.System system = entity.System.getInstance();
		
		Node createRequest = request.contents.getFirstChild();
		NamedNodeMap attributeMap = createRequest.getAttributes();
		
		// Parse attributes for the event
		String behavior  = attributeMap.getNamedItem("behavior").getNodeValue();
		String eventType = attributeMap.getNamedItem("type").getNodeValue();
		String question  = attributeMap.getNamedItem("question").getNodeValue();
		int numChoices = Integer.valueOf(attributeMap.getNamedItem("numChoices").getNodeValue());
		int numRounds  = Integer.valueOf(attributeMap.getNamedItem("numRounds").getNodeValue());
		
		// Parse users and choices
		NodeList children = createRequest.getChildNodes();
		ArrayList<User> users = new ArrayList<User>();
		ArrayList<String> choices = new ArrayList<String>();
		String name = null;
		String password;
		for(int i=0;i<children.getLength();i++){
			Node n = children.item(i);
			NamedNodeMap map = n.getAttributes();
			if(n.getNodeName().equalsIgnoreCase("user"))
			{
				name = map.getNamedItem("name").getNodeValue();
				password = map.getNamedItem("password").getNodeValue();
				users.add(new User(name,password));
			}
			else if(n.getNodeName().equalsIgnoreCase("choice"))
			{
				String value = map.getNamedItem("value").getNodeValue();
				int index = Integer.valueOf(map.getNamedItem("index").getNodeValue());
				choices.add(index, value);
			}
		}
		
		// Tell system to create a new event
		String eventId = null;
		if(eventType.equalsIgnoreCase("open"))
		{
			eventId = system.createOpenEvent(question, eventType, behavior, numChoices, numRounds, "init", users);
		}
		else if(eventType.equalsIgnoreCase("Closed"))
		{
			eventId = system.createClosedEvent(question, eventType, behavior, numChoices, numRounds, "init", users, choices);
		}
		
		// Associate this client with the given eventId
		ClientData cd = new ClientData(eventId,name);
		state.setData(cd);
		
		// Create response message
		
		// sample response: <response id = 'asdf' success='true'><createResponse id="asdf"/></response>
		NamedNodeMap requestMap = request.contents.getAttributes();
		String response = Message.responseHeader(requestMap.getNamedItem("id").getNodeValue());
		response += "<createResponse id='"+eventId+"' /></response>";
		
		// Return response
		return new Message(response);
	}

}

/* Relevant schema
<xs:element name='createRequest'> 
<xs:complexType>
  <!-- all choices, one at a time, as (string,index) entries -->
  <!-- moderator pre-signs in with the following user/password -->
  <xs:sequence>
    <xs:element ref='choice' minOccurs='0' maxOccurs='unbounded'/>
    <xs:element ref='user' minOccurs='1' maxOccurs='1'/>
  </xs:sequence>

  <xs:attribute name='behavior'   type="behaviorType" use="required"/>
  <xs:attribute name='type'       type='eventType'    use='required'/>
  <xs:attribute name='question'   type='xs:string'    use='required'/>
  <xs:attribute name='numChoices' type='xs:int'       use='required'/>
  <xs:attribute name='numRounds'  type='xs:int'       use='required'/>

</xs:complexType>
</xs:element>

<xs:element name='createResponse'> 
 <xs:complexType>
    <xs:attribute name='id' type='xs:string' use='required'/>   
  </xs:complexType>
</xs:element>
*/